/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.edu.cs576.HyperLinkVideo.authoringtool;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

/**
 *
 * @author Ludo
 */
public class PrimaryVideoViewPanel extends JPanel{
    private BufferedImage image;
    private int width = 352;
    private int height = 288;
    private byte[] videoBytes; //the bytes that will currently be shown on the panel
    private int totalFrames;
    private String currentVideo ="";
    
    private TrackingArea currentTrackingArea; 
    private boolean isKeyframe;
    
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (image != null) {
            g.clearRect(0, 0, width, height);
            g.drawImage(image, 0, 0, null);
            if(currentTrackingArea != null){
                if(isKeyframe){
                    g.setColor(Color.BLUE);
                }else{
                    g.setColor(Color.red);
                }
                g.drawPolygon(currentTrackingArea.getXPoints(), currentTrackingArea.getYPoints(), 4);
            }
            g.dispose();
        }

    }
    
    public void setCurrentTrackArea(TrackingArea area){
        currentTrackingArea = area;
        this.repaint();
    }
    
    public TrackingArea getCurrentTrackingArea(){
        return currentTrackingArea;
    }
    
    
        /**
     * Setting byte array of video to be displayed
     *
     * @param bytes
     */
    public void setBytes(byte[] bytes) {
        videoBytes = bytes;
        totalFrames = videoBytes.length/height/width/3-1;
        initImage();
    }
    
    public int getTotalNumberOfFrames(){
        if(videoBytes != null){
            return totalFrames;
        }else{
            return 0;
        }
    }
    
    public boolean isVideoSet(){
        if (videoBytes == null){
            return false;
        }else{
            return true;
        }
    }
    
    public void setFrame(int frameNum, boolean isKeyframe){
        image = getFrame(frameNum);
        this.isKeyframe = isKeyframe;
        this.repaint();
    }
    
    public BufferedImage getCurrentFrame(){
        return image;
    }
    
    /**
     * Picks frame to display of current video in jPanel. Make sure videoBytes
     * is not null.
     */
    public BufferedImage getFrame(int frame) {
        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        if (videoBytes == null) {
            System.out.println("video bytes is null.");
            return img;
        }
        int ind = frame * height * width * 3;
        for (int y = 0; y < height; y++) {

            for (int x = 0; x < width; x++) {

                byte a = 0;
                byte r = videoBytes[ind];
                byte g = videoBytes[ind + height * width];
                byte b = videoBytes[ind + height * width * 2];

                int pix = 0xff000000 | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
                //int pix = ((a << 24) + (r << 16) + (g << 8) + b);
                img.setRGB(x, y, pix);
                ind++;

            }
        }
        return img;
    }

    private void initImage() {
        image = getFrame(0);
        Graphics2D g2 = (Graphics2D) image.getGraphics();
    }
    
    public void setFilename(String filename){
        //System.out.println("Current Primary Video: "+filename);
        currentVideo = filename;
    }
    
    public String getFilename(){
        return currentVideo;
    }
}
